<style lang="scss" scoped>
.permission_wrap {
  width: 100%;
  padding: 20px;
  border: 1px solid rgb(217, 217, 217);
  border-radius: 4px;
  min-height: 200px;
  max-height: 300px;
  overflow-y: auto;
}
</style>
<template>
  <common-modal
    ref="modalRef"
    v-bind="attrs"
    @sure="search"
    @cancel="handleCancel"
    @before-close="beforeClose"
    @open="handleOpen"
  >
    <el-form
      label-width="110px"
      :rules="rules"
      ref="formRef"
      :inline="false"
      :model="formData"
    >
      <template v-if="type !== 'add'">
        <el-form-item label="mid" prop="mid" v-if="type !== 'addChild'">
          <el-input v-model="formData.mid" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="pmid" prop="pmid">
          <el-input v-model="formData.pmid" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="level" prop="level">
          <el-input v-model="formData.level" :disabled="true"></el-input>
        </el-form-item>
      </template>

      <el-form-item label="模块名称：" prop="mname">
        <el-input
          v-model="formData.mname"
          placeholder="请输入模块名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="API路由：" prop="path">
        <el-input
          v-model="formData.path"
          placeholder="请输入API路由"
          clearable
        />
      </el-form-item>
      <el-form-item label="前端路由：" prop="router">
        <el-input
          v-model="formData.router"
          placeholder="请输入前端路由"
          clearable
        />
      </el-form-item>
      <el-form-item label="类型：" prop="type">
        <el-radio-group v-model="formData.type">
          <el-radio label="NODE">NODE</el-radio>
          <el-radio label="URL">URL</el-radio>
          <el-radio label="HIDDEN">HIDDEN</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
  </common-modal>
</template>
<script>
export default {
  inheritAttrs: false,
};
</script>
<script setup>
import { reactive, useAttrs, ref } from "vue";
import commonModal from "@/components/commonModal.vue";
import useForm from "@/hooks/useForm";
import mockMenuList from "@/mock/menu";
import { ModuleSave } from "@/apis/module";
import { ElMessage } from "element-plus";
const attrs = useAttrs();
const props = defineProps({
  type: {
    type: String,
    default: "add",
  },
  curForm: {
    type: [Object, String],
    default: () => ({}),
  },
});
let formData = reactive({
  pmid: "ROOT",
  level: "",
  mname: "",
  path: "",
  router: "",
  type: "",
});
const modalRef = ref(null);
const emits = defineEmits(["success"]);
// 确定
const handleSure = async () => {
  await ModuleSave({ ...formData });
  ElMessage.success({
    message: "操作成功",
    type: "success",
  });
  emits("success");
  handleCancel();
};
const { formRef, search, reset } = useForm(handleSure);
const rules = {
  mname: [{ required: true, message: "请输入模块名称", trigger: "change" }],
  path: [{ required: true, message: "请输入API路由", trigger: "change" }],
  router: [{ required: true, message: "请输入前端路由", trigger: "change" }],
  type: [{ required: true, message: "请选择模块类型", trigger: "change" }],
};

// 取消 关闭弹窗
const handleCancel = () => {
  modalRef.value?.close();
};
// 打开弹窗
const handleOpen = () => {
  const { type, curForm } = props;
  if (type === "add") {
    Object.assign(formData, {
      mid: "",
      pmid: "ROOT",
      level: 1,
      mname: "",
      path: "",
      router: "",
      type: "",
    });
  } else {
    Object.assign(formData, curForm);
    if (type === 'addChild') {
      Object.assign(formData, {
        mname: "",
        path: "",
        router: "",
        type: "",
      });
    }
  }
};
const beforeClose = () => {
  reset()
}
</script>
